<?php
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\widget;


use think\facade\View;
use think\template\TagLib;

/**
 * 穿梭组件
 * @author 牧羊人
 * @since 2021/2/4
 * Class Transfer
 * @package app\admin\widget
 */
class Transfer extends TagLib
{
    // 自定义标签列表
    protected $tags = [
        'select' => ['attr' => 'param,data.value', 'close' => 0],
    ];

    /**
     * 日期选择标签
     * @param $tag 标签参数
     * @return string
     * @author 牧羊人
     * @since 2020/7/4
     */
    public function tagSelect($tag)
    {
        // 组件参数
        $param = trim($tag['param']);
        // 参数分析
        $item = explode("|", $param);
        // 组件名称
        $tsfName = $item[0];
        // 是否显示搜索
        $showSearch = $item[1];
        // 组件标题
        $tsfTitle = explode(",", $item[2]);
        // 组件显示名
        $showText = $item[3];
        // 组件显示值
        $showValue = $item[4];
        // 组件宽度
        $tsfWidth = 220;
        $tsfHeight = 350;
        if (isset($item[5]) && !empty($item[5])) {
            $subItem = explode("x", $item[5]);
            $tsfWidth = $subItem[0];
            $tsfHeight = $subItem[1];
        }

        // 组件数据源
        $data = $tag['data'];
        if (strpos($data, "=") !== false) {
            $result = explode(",", $data);
            $vData = [];
            foreach ($result as $v) {
                $subItem = explode("=", $v);
                $vData[$subItem[0]] = $subItem[1];
            }
            $data = $vData;
        }
        // 数据源重组
        $tsfData = array();
        foreach ($data as $key => $val) {
            $itemData = [
                'value' => $key,
                'title' => $val,
            ];
            $tsfData[] = $itemData;
        }

        // 组件值
        $value = $tag['value'];

        // 绑定数据
        View::assign("tsfName", $tsfName);
        View::assign("showSearch", $showSearch);
        View::assign("tsfTitle", $tsfTitle);
        View::assign("tsfWidth", $tsfWidth);
        View::assign("tsfHeight", $tsfHeight);
        View::assign("tsfData", json_encode($tsfData, JSON_UNESCAPED_UNICODE));
        View::assign("tsfValue", "<?php echo json_encode($value, JSON_UNESCAPED_UNICODE)?>");
        // 渲染模板
        return View::fetch("widget/transfer_select");
    }
}